home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / LSEQ101.ZIP / LSOPS.C < prev    next >
Text File  |  1990-06-20  |  4KB  |  164 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)lsops.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stdlib.h>*/
  9.  
  10. /* non-ansi headers */
  11. #include <bool.h>
  12.  
  13. /* library headers */
  14. #include <blkio.h>
  15.  
  16. /* local headers */
  17. #include "lseq_.h"
  18.  
  19. /*man---------------------------------------------------------------------------
  20. NAME
  21.      ls_alloc - allocate memory for a lseq
  22.  
  23. SYNOPSIS
  24.      #include "lseq_.h"
  25.  
  26.      int ls_alloc(lsp);
  27.      lseq_t *lsp;
  28.  
  29. DESCRIPTION
  30.      The ls_alloc function allocates the memory needed by lseq lsp.
  31.      The memory is initialized to 0.
  32.  
  33.      ls_alloc will fail if one or more of the following is true:
  34.  
  35.      [EINVAL]       lsp is not a valid lseq pointer.
  36.      [ENOMEM]       Not enough memory is available for
  37.                     allocation by the calling process.
  38.      [LSENOPEN]     lsp is not open.
  39.  
  40. SEE ALSO
  41.      ls_free.
  42.  
  43. DIAGNOSTICS
  44.      Upon successful completion, a value of 0 is returned.  Otherwise,
  45.      a value of -1 is returned, and errno set to indicate the error.
  46.  
  47. ------------------------------------------------------------------------------*/
  48. int ls_alloc(lsp)
  49. lseq_t *lsp;
  50. {
  51. #ifdef DEBUG
  52.     /* validate arguments */
  53.     if (!ls_valid(lsp)) {
  54.         LSEPRINT;
  55.         errno = EINVAL;
  56.         return -1;
  57.     }
  58.  
  59.     /* check if not open */
  60.     if (!(lsp->flags & LSOPEN)) {
  61.         LSEPRINT;
  62.         errno = LSENOPEN;
  63.         return -1;
  64.     }
  65.  
  66.     /* check for memory leak */
  67.     if (lsp->clsrp != NULL) {
  68.         LSEPRINT;
  69.         errno = LSEPANIC;
  70.         return -1;
  71.     }
  72. #endif
  73.     /* current record */
  74.     lsp->clsrp = ls_rcalloc(lsp);
  75.     if (lsp->clsrp == NULL) {
  76.         LSEPRINT;
  77.         return -1;
  78.     }
  79.  
  80.     errno = 0;
  81.     return 0;
  82. }
  83.  
  84. /*man---------------------------------------------------------------------------
  85. NAME
  86.      ls_blksize - lseq block size
  87.  
  88. SYNOPSIS
  89.      #include <lseq.h>
  90.  
  91.      size_t ls_blksize(lsp)
  92.      lseq_t *lsp;
  93.  
  94. DESCRIPTION
  95.      ls_blksize returns the size of the blocks in lseq lsp.  If lsp is
  96.      not a valid open lseq, the results are undefined.  ls_blksize is
  97.      a macro.
  98.  
  99. ------------------------------------------------------------------------------*/
  100. /* ls_blksize defined in lseq_.h. */
  101.  
  102. /*man---------------------------------------------------------------------------
  103. NAME
  104.      ls_free - free memory allocated for an lseq
  105.  
  106. SYNOPSIS
  107.      #include "lseq_.h"
  108.  
  109.      void ls_free(lsp)
  110.      lseq_t *lsp;
  111.  
  112. DESCRIPTION
  113.      The ls_free function frees all memory allocated for lseq lsp.
  114.      If lsp is not a valid lseq, no action is taken.
  115.  
  116. SEE ALSO
  117.      ls_alloc.
  118.  
  119. ------------------------------------------------------------------------------*/
  120. void ls_free(lsp)
  121. lseq_t *lsp;
  122. {
  123. #ifdef DEBUG
  124.     /* validate arguments */
  125.     if (!ls_valid(lsp)) {
  126.         LSEPRINT;
  127.         return;
  128.     }
  129. #endif
  130.     /* free memory */
  131.     ls_rcfree(lsp->clsrp);
  132.     lsp->clsrp = NULL;
  133.  
  134.     return;
  135. }
  136.  
  137. /*man---------------------------------------------------------------------------
  138. NAME
  139.      ls_valid - validate lseq
  140.  
  141. SYNOPSIS
  142.      #include "lseq_.h"
  143.  
  144.      bool ls_valid(lsp)
  145.      lseq_t *lsp;
  146.  
  147. DESCRIPTION
  148.      The ls_valid function determines if lsp is a valid lseq pointer.
  149.      If valid, then TRUE is returned.  If not, then FALSE is returned.
  150.  
  151. ------------------------------------------------------------------------------*/
  152. bool ls_valid(lsp)
  153. lseq_t *lsp;
  154. {
  155.     if (lsp < lsb || lsp > (lsb + LSOPEN_MAX - 1)) {
  156.         return FALSE;
  157.     }
  158.     if ((((char *)lsp - (char *)lsb)) % sizeof(*lsb) != 0) {
  159.         return FALSE;
  160.     }
  161.  
  162.     return TRUE;
  163. }
  164.